import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/shoppingCart.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class Shoppingcatmodel with ChangeNotifier {
  Map<int, Shoppingcart> shoppingcartMap =
      ShoppingcartMap(new Map<int, Shoppingcart>()).map;

  bool shoppingcartselectall = false;

  Future<void> init() async {
    var result = await Marketservice.getshoppingcartlist();
    shoppingcartMap = ShoppingcartMap.fromJson(result).map;
    notifyListeners();
  }

  void changeselectall() {
    shoppingcartselectall = !shoppingcartselectall;
    shoppingcartMap.forEach((int key, Shoppingcart value) {
      shoppingcartMap[key].selection = shoppingcartselectall;
    });
    print(shoppingcartselectall);
    notifyListeners();
  }

  Map<int, Shoppingcart> getshoppingcartMap() {
    return shoppingcartMap;
  }

  int getshoppingcatnum() {
    return shoppingcartMap.length;
  }

  int getshoppingcattotalnum() {
    int num = 0;
    shoppingcartMap.forEach((int key, Shoppingcart value) {
      num += shoppingcartMap[key].commodityNumber;
    });
    return num;
  }

  int getshoppingcatselectnum() {
    int num = 0;
    shoppingcartMap.forEach((int key, Shoppingcart value) {
      if (shoppingcartMap[key].selection)
        num += shoppingcartMap[key].commodityNumber;
    });
    return num;
  }

  double getshoppingcattotalprice() {
    double price = 0;
    shoppingcartMap.forEach((int key, Shoppingcart value) {
      if (shoppingcartMap[key].selection)
        price +=
            shoppingcartMap[key].money * shoppingcartMap[key].commodityNumber;
    });
    return price;
  }

  Future<void> addshop(int id, int num) async {
    EasyLoading.show(status: '请稍等...');
    bool flag = false;
    int shopid;
    shoppingcartMap.forEach((key, value) {
      if (value.commodityId == id) {
        flag = true;
        shopid = value.id;
      }
    });
    if (flag)
      await addshopcartitemnum(shopid,num);
    else {
      await Marketservice.addshop(id, num);
      await init();
    }
    notifyListeners();
    EasyLoading.dismiss();
  }

  void addshopcartitemnum(int id,int num) async {
    EasyLoading.show(status: '请稍等');
    for (int i = 0;i<num;i++) {
      await Marketservice.addshopnum(id);
    }
    
    if (shoppingcartMap[id].commodityNumber < 50)
      shoppingcartMap[id].commodityNumber+=num;
    print(shoppingcartMap[id].commodityNumber);
    notifyListeners();
    EasyLoading.dismiss();
  }

  void removeshopcartitemnum(int id) async {
    EasyLoading.show(status: '请稍等');
    await Marketservice.reduceshopnum(id);
    if (shoppingcartMap[id].commodityNumber > 1)
      shoppingcartMap[id].commodityNumber--;
    print(shoppingcartMap[id].commodityNumber);
    notifyListeners();
    EasyLoading.dismiss();
  }

  void deleteshopcart(int id) async {
    EasyLoading.show(status: '请稍等...');
    await Marketservice.deleteshop(id);
    shoppingcartMap.remove(id);
    notifyListeners();
    EasyLoading.dismiss();
  }

  void changeselect(int id) {
    bool flag = true;
    shoppingcartMap[id].selection = !shoppingcartMap[id].selection;
    shoppingcartMap.forEach((int key, Shoppingcart value) {
      if (!shoppingcartMap[key].selection) flag = false;
    });
    if (flag)
      shoppingcartselectall = true;
    else
      shoppingcartselectall = false;
    notifyListeners();
  }
}
